package cn.edu.gzhmt.user.admin.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.gzhmt.user.admin.service.IAdminUserService;
import cn.edu.gzhmt.user.admin.service.UserException;
import cn.edu.gzhmt.user.admin.service.impl.AdminUserServiceImpl;
import cn.edu.gzhmt.user.domain.User;
import cn.edu.gzhmt.utils.commons.CommonUtils;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

/**
 * 后台管里模块：web层
 * @author azhuo
 *
 */
public class AdminUserServlet extends BaseServlet {
	
	private IAdminUserService adminUserService = new AdminUserServiceImpl();

	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		
		// 进行用户登录
		try {
			User user = adminUserService.login(form);
			request.getSession().setAttribute("admin_user", user);
		} catch (UserException e) {
			String msg = e.getMessage();
			if(msg.startsWith("用户名")){
				request.setAttribute("msgUsername", e.getMessage());
			}else if(msg.startsWith("密码")){
				request.setAttribute("msgPassword", e.getMessage());
			}else{
				request.setAttribute("msg", e.getMessage());
			}
			return "f:/adminjsps/user/login.jsp";
		}
		
		return "r:/adminjsps/home.jsp";
	}
	
	/**
	 * 退出
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String quit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		session.setAttribute("user", null);
		session.invalidate();
		
		return "r:/adminjsps/user/login.jsp";
	}
	
	/**
	 * 查询所有用户
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<User> userList = adminUserService.findAll();
		request.setAttribute("userList", userList);
		
		return "f:/adminjsps/user/userList.jsp";
	}
	
	/**
	 * 查看所有管理员
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllAdmin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<User> adminList = adminUserService.findAllAdmin();
		request.setAttribute("adminList", adminList);
		
		return "f:/adminjsps/user/adminList.jsp";
	}
	
	/**
	 * 查询用户
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String username = request.getParameter("username");
		if(username == null){
			return null;
		}
		
		List<User> userList = adminUserService.findUserByUserName(username);
		request.setAttribute("userList", userList);
		
		return "f:/adminjsps/user/userList.jsp";
	}
	
	/**
	 * 查询管理员用户
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAdminUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String username = request.getParameter("username");
		if(username == null){
			return null;
		}
		
		List<User> adminList = adminUserService.findAdminUserByUserName(username);
		request.setAttribute("adminList", adminList);
		
		return "f:/adminjsps/user/adminList.jsp";
	}
	
	
	
}
